﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.Restaurant;

namespace FwProject.Manager
{
    public class RestaurantTypeNumberInfoManager : IDomainService
    {
        private readonly IRepository<RestaurantTypenNumberInfo, Guid> _restaurantTypeNumberRepository;
        public RestaurantTypeNumberInfoManager(IRepository<RestaurantTypenNumberInfo, Guid> restaurantTypeNumberRepository)
        {
            _restaurantTypeNumberRepository = restaurantTypeNumberRepository;

        }
        /// <summary>
        /// 添加一个餐厅购买型号
        /// </summary>
        /// <param name="typenumber"></param>
        /// <returns></returns>
        public async Task<Guid> CreateRestaurantTypeNumber(RestaurantTypenNumberInfo typenumber)
        {
            var model = _restaurantTypeNumberRepository.GetAll().Where(o => o.TypeNumberId == typenumber.TypeNumberId && o.Restaurantid == typenumber.Restaurantid ).FirstOrDefault();
            if (model != null)
            {
                if (model.IsDefault)
                {
                    throw new UserFriendlyException("当前餐厅已购买该型号");
                }

                else {
                    model.IsDefault = true;
                    return model.Id;
                }
            }
               
            return await _restaurantTypeNumberRepository.InsertAndGetIdAsync(typenumber);
        }
        /// <summary>
        ///修改一个餐厅购买型号
        /// </summary>
        /// <param name="typenumber"></param>
        /// <returns></returns>
        public async Task UpdateRestaurantTypeNumber(RestaurantTypenNumberInfo typenumber)
        {

            await _restaurantTypeNumberRepository.UpdateAsync(typenumber);
        }
        /// <summary>
        ///  delete 一个餐厅购买型号
        /// </summary>
        /// <param name="typenumber"></param>
        /// <returns></returns>
        public async Task DeleteRestaurantTypeNumber(RestaurantTypenNumberInfo typenumber)
        {

            await _restaurantTypeNumberRepository.DeleteAsync(typenumber);
        }
    }
}
